package com.example.courseservice.controller;

import com.example.courseservice.entity.Course;
import com.example.courseservice.entity.Chapter;
import com.example.courseservice.entity.Material;
import com.example.courseservice.service.CourseService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/courses")
public class CourseController {

    private final CourseService courseService;
    
    public CourseController(CourseService courseService) {
        this.courseService = courseService;
    }

    // 课程基础操作
    /**
     * 创建一个新的课程
     * @param course 要创建的课程对象
     * @return 创建成功后的课程对象及HTTP状态码201（CREATED）
     */
    @PostMapping
    public ResponseEntity<Course> createCourse(@RequestBody Course course) {
        Course createdCourse = courseService.createCourse(course);
        return new ResponseEntity<>(createdCourse, HttpStatus.CREATED);
    }

    /**
     * 根据课程ID获取课程信息
     * @param courseId 课程ID
     * @return 课程对象及HTTP状态码200（OK）
     */
    @GetMapping("/{courseId}")
    public ResponseEntity<Course> getCourseById(@PathVariable String courseId) {
        Course course = courseService.getCourseById(courseId);
        return ResponseEntity.ok(course);
    }

    /**
     * 更新课程信息
     * @param courseId 课程ID
     * @param course 更新的课程对象
     * @return 更新后的课程对象及HTTP状态码200（OK）
     */
    @PutMapping("/{courseId}")
    public ResponseEntity<Course> updateCourse(@PathVariable String courseId, @RequestBody Course course) {
        Course updatedCourse = courseService.updateCourse(courseId, course);
        return ResponseEntity.ok(updatedCourse);
    }

    /**
     * 删除课程
     * @param courseId 课程ID
     * @return HTTP状态码204（NO_CONTENT）
     */
    @DeleteMapping("/{courseId}")
    public ResponseEntity<Void> deleteCourse(@PathVariable String courseId) {
        courseService.deleteCourse(courseId);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 获取所有课程
     * @return 所有课程的列表及HTTP状态码200（OK）
     */
    @GetMapping
    public ResponseEntity<List<Course>> getAllCourses() {
        return ResponseEntity.ok(courseService.getAllCourses());
    }

    // 章节管理
    /**
     * 为指定课程添加一个章节
     * @param courseId 课程ID
     * @param chapter 要添加的章节对象
     * @return HTTP状态码201（CREATED）
     */
    @PostMapping("/{courseId}/chapters")
    public ResponseEntity<Void> addChapter(@PathVariable String courseId, @RequestBody Chapter chapter) {
        courseService.addChapter(courseId, chapter);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    /**
     * 删除指定课程的指定章节
     * @param courseId 课程ID
     * @param chapterId 章节ID
     * @return HTTP状态码204（NO_CONTENT）
     */
    @DeleteMapping("/{courseId}/chapters/{chapterId}")
    public ResponseEntity<Void> deleteChapter(@PathVariable String courseId, @PathVariable String chapterId) {
        courseService.deleteChapter(courseId, chapterId);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 获取指定课程的所有章节
     * @param courseId 课程ID
     * @return 章节列表及HTTP状态码200（OK）
     */
    @GetMapping("/{courseId}/chapters")
    public ResponseEntity<List<Chapter>> getChapters(@PathVariable String courseId) {
        return ResponseEntity.ok(courseService.getChapters(courseId));
    }

    // 课程资料管理
    /**
     * 为指定课程的指定章节添加一个资料
     * @param courseId 课程ID
     * @param chapterId 章节ID
     * @param material 要添加的资料对象
     * @return HTTP状态码201（CREATED）
     */
    @PostMapping("/{courseId}/chapters/{chapterId}/materials")
    public ResponseEntity<Void> addMaterial(
            @PathVariable String courseId,
            @PathVariable String chapterId,
            @RequestBody Material material) {
        courseService.addMaterial(courseId, chapterId, material);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    /**
     * 删除指定课程的指定章节的指定资料
     * @param courseId 课程ID
     * @param chapterId 章节ID
     * @param materialId 资料ID
     * @return HTTP状态码204（NO_CONTENT）
     */
    @DeleteMapping("/{courseId}/chapters/{chapterId}/materials/{materialId}")
    public ResponseEntity<Void> deleteMaterial(
            @PathVariable String courseId,
            @PathVariable String chapterId,
            @PathVariable String materialId) {
        courseService.deleteMaterial(courseId, chapterId, materialId);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
}